﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.CopsBanditsUI {
    import flash.display.*;
    import Events.*;
    import org.aswing.*;
    import org.aswing.event.*;
    import org.aswing.ext.*;
    import Display.aswingui.*;

    public class UpgradesScrollingList extends JPanel {

        public static const NUM_ITEMS:int = 4;

        protected const ROW_WIDTH:int;
        protected const ROW_HEIGHT:int;

        protected var m_cellFactory:GridListCellFactory;
        protected var m_dataList:GridList;
        protected var m_data:Array;
        protected var m_scrollPane:JScrollPane;
        protected var prevButton:JButton;
        protected var nextButton:JButton;
        protected var m_columns:int;
        protected var m_rows:int;
        protected var m_model:VectorListModel;
        protected var m_curCount:int = 0;
        protected var m_curIndex:int = 0;
        protected var m_initNumItems:int;

        public function UpgradesScrollingList(_arg1:Array, _arg2:Class, _arg3:int, _arg4:int=0, _arg5:int=1, _arg6:int=4){
            this.ROW_WIDTH = (UpgradesCell.CELL_WIDTH * NUM_ITEMS);
            this.ROW_HEIGHT = UpgradesCell.CELL_HEIGHT;
            super(new SoftBoxLayout(SoftBoxLayout.X_AXIS, 0, SoftBoxLayout.CENTER));
            this.m_cellFactory = new (_arg2)();
            this.m_columns = _arg4;
            this.m_rows = _arg5;
            this.m_data = _arg1;
            this.m_initNumItems = _arg6;
        }
        public function create():void{
            this.makeScroller();
            this.makeButtons();
            this.makeSize();
            this.append(this.prevButton);
            this.appendAll(ASwingHelper.horizontalStrut(10), this.m_scrollPane, ASwingHelper.horizontalStrut(10));
            this.append(this.nextButton);
            this.addBorder();
            ASwingHelper.prepare(this);
        }
        protected function addBorder():void{
            var _local1:Sprite = new Sprite();
            var _local2:DisplayObject = (new (CopsDialog.assetDict["police_innerBorder"])() as DisplayObject);
            _local2.width = 597;
            _local2.height = 343;
            _local2.x = 39;
            _local2.y = -1.5;
            _local1.addChild(_local2);
            _local1.mouseChildren = (_local1.mouseEnabled = false);
            var _local3:DisplayObject = (new (CopsDialog.assetDict["police_divider"])() as DisplayObject);
            _local3.x = 186;
            _local3.y = 50;
            var _local4:DisplayObject = (new (CopsDialog.assetDict["police_divider"])() as DisplayObject);
            _local4.x = 333;
            _local4.y = 50;
            var _local5:DisplayObject = (new (CopsDialog.assetDict["police_divider"])() as DisplayObject);
            _local5.x = 481;
            _local5.y = 50;
            _local1.addChild(_local3);
            _local1.addChild(_local4);
            _local1.addChild(_local5);
            addChild(_local1);
        }
        protected function makeScroller():void{
            this.m_model = new VectorListModel();
            var _local1:int = Math.min(this.m_initNumItems, this.m_data.length);
            var _local2:int;
            while (_local2 < _local1) {
                this.m_model.append(this.m_data[_local2]);
                this.m_curCount++;
                _local2++;
            };
            this.m_dataList = new GridList(this.m_model, this.m_cellFactory, this.m_columns, this.m_rows);
            this.m_dataList.setHGap(0);
            this.m_scrollPane = new JScrollPane(this.m_dataList, JScrollPane.SCROLLBAR_NEVER, JScrollPane.SCROLLBAR_NEVER);
            this.m_dataList.addEventListener(UIEvent.CHANGE_CREW_STATE, this.refreshUpperCells, false, 0, true);
        }
        protected function refreshUpperCells(_arg1:UIEvent):void{
            var _local3:int;
            var _local5:UpgradesCell;
            var _local2:UpgradesCell = (_arg1.target as UpgradesCell);
            var _local4:int;
            while (_local4 < this.m_data.length) {
                if (_local2.cellValue == this.m_data[_local4]){
                    _local3 = (_local4 + 1);
                    if (this.m_data[_local3].newItemName != null){
                        _local5 = (this.m_dataList.getCellByIndex(_local3) as UpgradesCell);
                        if (_local5 != null){
                            _local5.enableUpgradeButton();
                        };
                    };
                };
                _local4++;
            };
        }
        protected function makeButtons():void{
            var _local1:DisplayObject = (new (CopsDialog.assetDict["police_prevButton_up"])() as DisplayObject);
            var _local2:DisplayObject = (new (CopsDialog.assetDict["police_prevButton_down"])() as DisplayObject);
            var _local3:DisplayObject = (new (CopsDialog.assetDict["police_prevButton_over"])() as DisplayObject);
            var _local4:DisplayObject = (new (CopsDialog.assetDict["police_nextButton_up"])() as DisplayObject);
            var _local5:DisplayObject = (new (CopsDialog.assetDict["police_nextButton_down"])() as DisplayObject);
            var _local6:DisplayObject = (new (CopsDialog.assetDict["police_nextButton_over"])() as DisplayObject);
            this.prevButton = new JButton();
            this.prevButton.wrapSimpleButton(new SimpleButton(_local1, _local3, _local2, _local1));
            this.prevButton.setEnabled(false);
            this.nextButton = new JButton();
            this.nextButton.wrapSimpleButton(new SimpleButton(_local4, _local6, _local5, _local4));
            if (this.m_data.length <= NUM_ITEMS){
                this.nextButton.setEnabled(false);
            } else {
                this.nextButton.setEnabled(true);
            };
            ASwingHelper.setEasyBorder(this.nextButton, 0, 0, 31);
            ASwingHelper.setEasyBorder(this.prevButton, 0, 0, 31, 0);
            this.prevButton.addActionListener(this.moveLeft, 0, true);
            this.nextButton.addActionListener(this.moveRight, 0, true);
        }
        protected function makeSize():void{
            this.setPreferredHeight(this.ROW_HEIGHT);
            ASwingHelper.setForcedWidth(this.m_scrollPane, this.ROW_WIDTH);
            this.m_dataList.setHGap(0);
        }
        protected function moveLeft(_arg1:AWEvent):void{
            this.m_curIndex = (this.m_curIndex - NUM_ITEMS);
            if (this.m_curIndex <= 0){
                this.prevButton.setEnabled(false);
                this.m_curIndex = 0;
            };
            this.m_dataList.scrollHorizontal(-(this.ROW_WIDTH));
            this.nextButton.setEnabled(true);
        }
        protected function moveRight(_arg1:AWEvent):void{
            var _local4:int;
            var _local2:int = this.m_curCount;
            var _local3:int = Math.min(this.m_data.length, (this.m_curCount + NUM_ITEMS));
            if ((_local3 - _local2) > 0){
                if (this.m_model.getSize() < (NUM_ITEMS + this.m_curCount)){
                    _local4 = _local2;
                    while (_local4 < _local3) {
                        this.m_model.append(this.m_data[_local4]);
                        this.m_curCount++;
                        this.m_curIndex++;
                        _local4++;
                    };
                    if (this.m_curIndex >= (this.m_data.length - NUM_ITEMS)){
                        this.nextButton.setEnabled(false);
                    };
                };
            } else {
                if (this.m_curIndex < this.m_curCount){
                    this.m_curIndex = (this.m_curIndex + NUM_ITEMS);
                    if (this.m_curIndex >= (this.m_data.length - NUM_ITEMS)){
                        this.nextButton.setEnabled(false);
                        this.m_curIndex = (this.m_data.length - NUM_ITEMS);
                    };
                };
            };
            this.prevButton.setEnabled(true);
            this.m_dataList.scrollHorizontal(this.ROW_WIDTH);
        }

    }
}//package Display.CopsBanditsUI 
